package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.DictTool;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.WrongEmpCode;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@BusinessDataConverter(
    auto = false,
    insertSql = "INSERT INTO ACT_HI_TASKINST\n" +
        "(ID_, PROC_DEF_ID_, TASK_DEF_KEY_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, " +
        "ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, PRIORITY_, DUE_DATE_, FORM_KEY_, CATEGORY_, TENANT_ID_)\n" +
        "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
    updateSql = "UPDATE ACT_HI_TASKINST\n" +
        "SET PROC_DEF_ID_=?, TASK_DEF_KEY_=?, PROC_INST_ID_=?, EXECUTION_ID_=?, NAME_=?, PARENT_TASK_ID_=?, DESCRIPTION_=?, OWNER_=?, ASSIGNEE_=?, START_TIME_=?, CLAIM_TIME_=?, END_TIME_=?, DURATION_=?, DELETE_REASON_=?, PRIORITY_=?, DUE_DATE_=?, FORM_KEY_=?, CATEGORY_=?, TENANT_ID_=?\n" +
        "WHERE ID_ = ?",
    findMigratedIdListSql = "SELECT ID_ FROM ACT_HI_TASKINST WHERE TENANT_ID_  = ? \n"
)
public class ActHiTaskinst extends AbstractBusiness {

  public ActHiTaskinst(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
  }

  @Override
  public void migrate() throws Exception {
  }

  public void customMigrate(Map procInstRow) throws Exception {
    int itemSencorLoanHnyTaskCounter = 0;
    for(int i = 0,j=this.sourceData.getJbpmTaskInstanceList().size(); i< j;i++) {
      Map row = this.sourceData.getJbpmTaskInstanceList().get(i);
      if(!row.get("PROCINST_").toString().equals(procInstRow.get("ID_").toString())){
        continue;
      }
      try {
        if(!procInstRow.get("OPEN_TASK_COUNT").toString().equals("0")
            && procInstRow.get("WFNAME").toString().equals("item-sencor-loan-hny")
            && itemSencorLoanHnyTaskCounter >= 1) {
          break;
        }
        // 判断是否需要覆盖迁移
        int needMigratedResult = this.needMigrated(row.get("ID_") + "-JW");
        if(needMigratedResult == 0) {
          continue;
        }

        if(needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql, this.buildValues(row,procInstRow), true);
//          this.batchUpdateValues.add(this.buildValues(row));
        } else {
            this.targetDbUtil.updateByParams(insertSql, this.buildValues(row,procInstRow), false);
//          this.batchInsertValues.add(this.buildValues(row));
        }
        itemSencorLoanHnyTaskCounter ++;
      } catch (Exception e) {
        throw new Exception(String.format("任务实例[%s]迁移失败:%s\n", row.get("ID_"), e.getMessage()));
      }
    }
  }

  public List buildValues(Map row,Map procInstRow) throws SQLException {
    String taskInstId = row.get("ID_")+"-JW";

    List values = new ArrayList();
    values.add(taskInstId);

    if(taskInstId.equals("37286054-JW")) {
      System.out.println("hello");
    }
    values.add(BusinessTool.getProcDefId(procInstRow));
    String[] taskDefNameAndKey = BusinessTool.getTaskDefNameAndKey(procInstRow,
        row.get("PROCDEF_NAME_").toString(),row.get("TASK_DEF_NAME_").toString());
    values.add(taskDefNameAndKey[1]);
    values.add(row.get("PROCINST_")+"-JW");
    values.add(row.get("PROCINST_")+"-JW");
    values.add(taskDefNameAndKey[0]);
    // PARENT_TASK_ID_
    values.add(row.get("EMPNAME"));
    // TODO:DESCRIPTION_
    values.add(null);
    // OWNER_
    values.add(row.get("ACTORID_"));
    //ASSIGNEE_
    values.add((row.get("ACTORID_") == null || row.get("ACTORID_").toString().equals("")) ? null : BusinessTool.getTaskRealActorId(row.get("ACTORID_").toString(), row.get("CREATE_")));
    // START_TIME_
    values.add(row.get("CREATE_"));
    // CLAIM_TIME_
    values.add(null);
    // END_TIME
    values.add(row.get("END_"));
    //DURATION_
    if(row.get("END_") != null && !row.get("END_").toString().equals("")) {
      Timestamp createTs = (Timestamp) row.get("CREATE_");
      Timestamp endTs = (Timestamp) row.get("END_");
      values.add(endTs.getTime() - createTs.getTime());
    } else {
      values.add(null);
    }
    // TODO:DELETE_REASON_
    values.add("completed");
    // PRIORITY_
    values.add(50);
    // DUE_DATE_
    values.add(null);
    //TODO:FORM_KEY_
    values.add(null);
    //CATEGORY_
    values.add(null);
    //TENANT_ID_
    values.add(this.projectId);

    return values;

  }

}
